<?php
namespace App\Traits;

use App\Http\Controllers\HandleRedis;

/**
 * userToken的存取
 * Trait UserToken
 * @package App\Traits
 */
trait UserToken
{
    protected static $userTokenExpire = 30 * 60;//设置userToken失效期

    protected static $userTokenKeyPre = 'token_';//userToken前缀

    protected static $userToken;//用户token

    protected static $userData = [];//用户数据

    protected static $userId = '';//用户id

    protected static $waterSetting = 100;//设置流水金额满100提取

    protected static function setUserToken($data = [])
    {
        if($data) {
            $data['token'] = self::$userToken = self::createUserToken();
            HandleRedis::connectWorker()->hmset(self::$userToken,$data);
            HandleRedis::connectWorker()->expire(self::$userToken,self::$userTokenExpire);
            return $data;
        }
    }

    protected static function getUserToken()
    {
        return HandleRedis::connectWorker()->hGetAll(self::$userToken);
    }

    protected static function createUserToken()
    {
        return static::$userTokenKeyPre . md5((((rand() % 8999)) + 1000) . time());
    }

    /**
     * 更新用户数据
     * @param array $data
     */
    protected static function updateUserData($data = [])
    {
        if ($data) HandleRedis::connectWorker()->hmset(self::$userToken,$data);
    }
}
